﻿using BattleBotsMattAndSunny.Components.Data;
using BattleBotsMattAndSunny.Components.Guns;
using BattleBotsMattAndSunny.Components.Interfaces;
using BattleBotsMattAndSunny.Components.Radar;
using BattleBotsMattAndSunny.Components.Treads;
using Robocode;
using System;
using System.Drawing;

namespace BattleBotsMattAndSunny
{
    public class Ramrod : AdvancedRobot
    {
        public IDataManager Data { get; set; }

        public IRadarManager Radar { get; set; }

        public IGunManager Gun { get; set; }

        public ITreadsManager Treads { get; set; }

        public override bool IsAdjustGunForRobotTurn { get { return true; } }

        public Ramrod()
        {
            Data = new DataManager();
            Radar = new LockingRadar(Data, this);
            Gun = new BasicGunner(Data, this);
            Treads = new ChaseTreads(Data, this);
        }

        public override void Run()
        {
            IsAdjustRadarForRobotTurn = false;
            IsAdjustRadarForGunTurn = false;
            while (true)
            {
                BodyColor = Color.DarkRed;
                GunColor = Color.DarkRed;
                BulletColor = Color.DarkRed;
                RadarColor = Color.DarkRed;
                ScanColor = Color.DarkRed;
                Treads.Move();
                Gun.Move();
                Radar.Move();
                if (DistanceRemaining > 0 || TurnRemaining > 0 || GunTurnRemaining > 0 || RadarTurnRemaining > 0)
                    Execute();
            }
        }

        public override void OnRobotDeath(RobotDeathEvent e)
        {
            if (e.Name == Data.GetLastScan().Name)
                Radar = new LockingRadar(Data, this);
        }

        public override void OnScannedRobot(ScannedRobotEvent e)
        {
            BodyColor = Color.HotPink;
            GunColor = Color.HotPink;
            BulletColor = Color.HotPink;
            RadarColor = Color.HotPink;
            ScanColor = Color.HotPink;
            if (e != null)
            {
                try
                {
                    Radar.Scan(e);
                    if (Energy > 10)
                    {
                        if (e.Distance > 250)
                            Gun.OnScannedRobot(e, 1);
                        else if (e.Distance > 150 && e.Distance <= 250)
                            Gun.OnScannedRobot(e, 2);
                        else if (e.Distance > 100 && e.Distance <= 150)
                            Gun.OnScannedRobot(e, 4);
                        else
                            Gun.OnScannedRobot(e, 5);
                    }
                    else
                        Gun.OnScannedRobot(e, Math.Min(1, Energy / 2));
                    Treads.OnScannedRobot(e);
                }
                catch (Exception ex)
                {
                    Out.WriteLine(ex.Message);
                }
            }
            base.OnScannedRobot(e);
        }

        public override void OnHitByBullet(HitByBulletEvent evnt)
        {
            try
            {
                Treads.OnHitByBullet(evnt);
                //todo add radar response
            }
            catch (Exception ex)
            {
                Out.WriteLine(ex.Message);
            }
        }

        public override void OnHitRobot(HitRobotEvent e)
        {
            //SetBack(25);
        }

        public override void OnHitWall(HitWallEvent e)
        {
            try
            {
                Treads.OnHitWall(e);
            }
            catch
            {
            }
        }
    }
}